<template>
  <div :style="{ height: '98vh', display: 'flex', alignItems: 'center', justifyContent: 'center', backgroundImage: `url(${require('@/assets/background.png')})`, backgroundSize: 'cover' }">
    <div style="display: flex; background-color: white; width: 50%; border-radius: 8px; overflow: hidden">
      <div style="flex: 1;">
        <img src="@/assets/login.png" alt="" style="width: 100%">
      </div>
      <div style="flex: 1; display: flex; align-items: center; justify-content: center;">
        <!-- 表单 -->
        <el-form :model="user" style="width:70%" :rules="rules" ref="loginRef">
          <div style="font-size: 25px; font-weight: bold; text-align: center; margin-bottom: 20px">欢迎使用学生管理系统</div>

          <!-- 选择身份 -->
          <el-form-item prop="role" style="margin-bottom: 16px;">
            <el-select v-model="user.role" placeholder="请选择身份" style="width: 100%; height: 40px;">
              <el-option label="学生" value="student"></el-option>
              <el-option label="老师" value="teacher"></el-option>
              <el-option label="管理员" value="admin"></el-option>
            </el-select>
          </el-form-item>
          <!-- 用户名 -->
          <el-form-item prop="username" style="margin-bottom: 16px;">
            <el-input prefix-icon="el-icon-user" size="medium" placeholder="请输入账号" v-model="user.username" style="width: 100%; height: 40px;"></el-input>
          </el-form-item>
          <!-- 密码 -->
          <el-form-item prop="password" style="margin-bottom: 16px;">
            <el-input prefix-icon="el-icon-lock" size="medium" show-password placeholder="请输入密码" v-model="user.password" style="width: 100%; height: 40px;"></el-input>
          </el-form-item>
          <!-- 验证码 -->
          <el-form-item prop="code" style="margin-bottom: 20px;">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-circle-check" size="medium" style="flex: 1; height: 40px;" placeholder="请输入验证码" v-model="user.code"></el-input>
              <div style="flex: 1; display: flex; justify-content: flex-end;">
                <ValidCode ref="validCodeComponent" @input="getCode" style="background-color: aliceblue; height: 40px; "></ValidCode>
              </div>
            </div>
          </el-form-item>
          <!-- 登录按钮 -->
          <el-form-item style="margin-bottom: 16px;">
            <el-button type="primary" style="width: 100%; height: 40px;" @click="login">登 录</el-button>
          </el-form-item>
          <!-- 忘记密码按钮 -->
          <div style="text-align: right; width: 100%; margin-top: -5px;">
            <span style="font-size: 13px; color: #409EFF; cursor: pointer" @click="handleForgetPass" >忘记密码</span>
          </div>
        </el-form>
      </div>
    </div>

    <!-- 弹窗 -->
    <el-dialog title="忘记密码" :visible.sync="forgetPassDialogVis" width="30%">
      <el-form :model="forgetUserForm" label-width = "80px" style="padding-right: 20px">
        <el-form-item label="用户名">
          <el-input v-model="forgetUserForm.name" autocomplete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="forgetUserForm.email" autocomplete="off" placeholder="请输入绑定的邮箱重置密码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="forgetPassDialogVis = false">取 消</el-button>
        <el-button type="primary" @click="resetPassword">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ValidCode from '@/components/ValidCode.vue'

export default {
  name: "Login.vue",
  components: {
    ValidCode
  },
  data() {
    // 验证码判断
    const validateCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'));
      } else if (value.toLowerCase() !== this.code.toLowerCase()){
        callback(new Error('验证码输入错误'));
        this.refreshValidCode(); // 刷新验证码
      } else {
        callback();
      }
    };
    return {
      forgetUserForm: {

      },
      forgetPassDialogVis: false, //弹窗是否可见
      code: '', // 验证码组件的值
      user: {
        role: '',
        username: '',
        password: '',
        code: '', // 输入的验证码
      },
      rules: {
        role: [
          { required: true, message: '请选择身份', trigger: 'change' },
        ],
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        code: [
          { validator: validateCode, trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    refreshValidCode() { // 刷新验证码
      this.$refs.validCodeComponent.refreshCode();
    },
    handleForgetPass(){ //初始化表单
      this.forgetUserForm = {};
      this.forgetPassDialogVis = true;
    },
    resetPassword(){
      //重置密码逻辑
    },
    getCode(code) {
      this.code = code
    },
    login() {
      // 验证表单
      this.$refs['loginRef'].validate((valid) => {
        if (valid) {
          // 验证通过，发送请求
          this.$request.post('/login', this.user).then(res => {
            console.log(res)
            if(res.code === '200'){
              this.$router.push('/')
              this.$message.success('登录成功')
              localStorage.setItem("user-token",JSON.stringify(res.data)) // 储存用户数据
            }else{
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
  },
};
</script>

<style>
</style>
